package com.siwei.mes.service.laboratory.factory.concrete;

import com.alibaba.fastjson.JSONObject;
import com.siwei.mes.entity.experiment.CheckBatchConfig;
import com.siwei.mes.entity.experiment.Experiment;
import com.siwei.mes.entity.experiment.ExperimentDetail;
import com.siwei.mes.entity.system.TestProjectInfo;
import com.siwei.mes.entity.system.TestProjectSpecConfig;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.mapper.experiment.ExperimentMapper;
import com.siwei.mes.mapper.system.TestProjectInfoMapper;
import com.siwei.mes.service.laboratory.factory.entity.LaboratoryDownloadVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 混凝土上传到协会工厂主方法
 *
 * @Description: 混凝土上传到协会工厂主方法
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/3/28 22:10
 */
@Service
public class ConcreteLaboratoryFactoryService {
    private static final Logger log = LoggerFactory.getLogger("laboratory");
    @Resource
    private TestProjectInfoMapper testProjectInfoMapper;
    @Resource
    private ExperimentMapper experimentMapper;

    /**
     * 处理上传
     *
     * @param objJson                   上传的json数据
     * @param sampleNo                  样品编码
     * @param experiment                试验委托信息
     * @param batchConfigList           批检指标配置
     * @param testProjectSpecConfigList 试验项目材料规格判定配置
     * @param detail                    试验委托详情信息
     */
    public void processUpload(JSONObject objJson,
                              String sampleNo,
                              Experiment experiment,
                              List<CheckBatchConfig> batchConfigList,
                              List<TestProjectSpecConfig> testProjectSpecConfigList,
                              ExperimentDetail detail) {
        String testProjectCode = detail.getTestProjectCode();
        if (StringUtils.isBlank(testProjectCode)) {
            log.error("未找到对应试验项目code：【{}】", testProjectCode);
            return;
        }
        log.info("查找混凝土工厂选择器，对应试验项目code：【{}】", testProjectCode);
        ConcreteLaboratoryFactory factory = ConcreteLaboratoryFactorySelector.getFactory(testProjectCode);
        if (factory != null) {
            factory.processUpload(objJson, sampleNo, experiment, batchConfigList, testProjectSpecConfigList, detail);
        }
        //else {
        //    // 处理未匹配到工厂的情况
        //    log.error("未找到匹配的混凝土工厂：【{}】，不需要上传!", testProjectCode);
        //}
    }

    /**
     * 处理下载
     *
     * @param sampleNo             样品编码
     * @param experimentDetailList 试验委托详情信息
     * @param experiment           实验信息
     * @param wtSampleList         协会数据
     */
    public void processDownload(String sampleNo, List<ExperimentDetail> experimentDetailList, Experiment experiment, Map<String, Object> wtSampleList) {
        List<TestProjectInfo> testProjectInfoList = testProjectInfoMapper.getList(new TestProjectInfo().setTestType(MaterialsEnum.CONCRETE.getTestProjectType()));
        //获取到所有试验项目
        String[] examParameterCn = wtSampleList.get("Exam_parameter_Cn").toString().split(";");
        String[] status = wtSampleList.get("Status").toString().split(";");
        Map<String, String> parameterMap = new HashMap<>();
        for (int i = 0; i < examParameterCn.length; i++) {
            if (StringUtils.isNotBlank(examParameterCn[i]) && StringUtils.isNotBlank(status[i])) {
                //根据试验项目查询到对照的品控试验项目名称
                for (TestProjectInfo testProjectInfo : testProjectInfoList) {
                    if (StringUtils.isNotBlank(testProjectInfo.getParameterName())) {
                        if (testProjectInfo.getParameterName().contains(examParameterCn[i])) {
                            parameterMap.put(testProjectInfo.getTestCode(), status[i]);
                        }
                    }
                }
            }
        }

        for (ExperimentDetail detail : experimentDetailList) {
            String testProjectCode = detail.getTestProjectCode();
            if (StringUtils.isBlank(testProjectCode)) {
                log.error("未找到对应试验项目code：【{}】", testProjectCode);
                return;
            }
            log.info("查找混凝土工厂选择器，对应试验项目code：【{}】", testProjectCode);
            ConcreteLaboratoryFactory factory = ConcreteLaboratoryFactorySelector.getFactory(testProjectCode);
            if (factory != null) {
                LaboratoryDownloadVo downloadVo = factory.processDownload(sampleNo, experiment, detail, parameterMap);
                if (downloadVo == null) {
                    continue;
                }
                //修改台账主表数据
                experiment.setExperimentGist(downloadVo.getExperimentGist());
                experiment.setJudgeGist(downloadVo.getJudgeGist());
                experiment.setEquipment(downloadVo.getEquipment());
                experiment.setTemperature(downloadVo.getTemperature());
                experiment.setHumidness(downloadVo.getHumidness());
                experimentMapper.updateByPrimaryKeySelective(experiment);
            }
            //else {
            //    // 处理未匹配到工厂的情况
            //    log.error("未找到匹配的混凝土工厂：【{}】，不需要上传!", testProjectCode);
            //}
        }
    }
}
